package com.contact;

import java.util.ArrayList;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.adpter.FriendsApplyListAdapter;
import com.android.ocean.util.common.StringUtil;
import com.android.ocean.util.upgrade.DialogHelper;
import com.http.FriendApplyHandleTask;
import com.matang.R;
import com.modle.ApplyFriends;
import com.util.AppUtil;

/**
 * 
 * @ClassName:FriendApplyListActivity
 * @Description:TODO申请好友列表
 * @author zhuw
 * @date 2013-11-19 下午10:26:44
 */
public class FriendApplyListActivity extends BaseActivity {
	ListView listView;
	ArrayList<ApplyFriends> applyList;
	FriendsApplyListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_yrlist);
		init();
	}

	void init() {
		mContext = FriendApplyListActivity.this;
		findViews();
		initPaneData();

	}

	public void initPaneData() {
		super.initPaneData();
		header.headTitleTv.setText("好友申请列表");
		applyList = (ArrayList<ApplyFriends>) getIntent().getSerializableExtra(
				"applyList");
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int pos, long arg3) {
				final int delPos = pos;
				DialogHelper.Confirm(mContext, "确定删除?", null, "确定",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								handle(applyList.get(delPos), "no");
								applyList.remove(delPos);

							}
						}, "取消", new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();

							}
						});
				return false;
			}
		});
		fillList();
	}

	@Override
	protected void findViews() {
		super.findViews();
		listView = (ListView) findViewById(R.id.listView);

	}

	void fillList() {
		if (StringUtil.isListNull(applyList)) {
			AppUtil.tsMsg(mContext, "无数据...");
		} else {
			adapter = new FriendsApplyListAdapter(applyList, mContext);
			listView.setAdapter(adapter);

		}

	}

	/**
	 * 
	 * @Description:TODO
	 * @param@param af
	 * @param@param flag = "yes" 或 "no"
	 * @return void
	 * @author zhuw
	 * @date 2013-11-19 下午11:01:51
	 */
	void handle(ApplyFriends af, String flag) {
		AppUtil.tsMsg(mContext, "处理中,请稍后");
		new FriendApplyHandleTask(callBack).execute(af.id, flag);
	}

	FriendApplyHandleTask.CallBack callBack = new FriendApplyHandleTask.CallBack() {

		@Override
		public void procError(String msg) {
			AppUtil.tsMsg(mContext, msg);
			adapter.notifyDataSetChanged();

		}

		@Override
		public void procData(String str) {
			AppUtil.tsMsg(mContext, str);
			adapter.notifyDataSetChanged();

		}
	};

}
